package com.example.lib_cache.viewmodel;

import android.util.Log;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.baidu.mapapi.search.sug.SuggestionResult;
import com.example.lib_cache.R;
import com.example.lib_cache.model.Address;
import com.example.lib_cache.model.Poi;
import com.example.lib_common.util.LiveDataManager;

import java.util.ArrayList;
import java.util.List;

public class StartActivityViewModel extends ViewModel {

    private MutableLiveData<Address> startAddressLiveData;

    private MutableLiveData<List<Poi>> startPoiListLiveData;

    public MutableLiveData<Address> getStartAddressLiveData() {
        return startAddressLiveData;
    }

    public void setStartAddressLiveData(Address address) {
        this.startAddressLiveData = LiveDataManager
                .getInstance().with("startAddress",Address.class);
        this.startAddressLiveData.postValue(address);

    }


    public MutableLiveData<List<Poi>> getStartPoiListLiveData() {
        return startPoiListLiveData;
    }

    public void setStartPoiListLiveData(List<Poi> poiList) {
        this.startPoiListLiveData = new MutableLiveData<>();
        this.startPoiListLiveData.setValue(poiList);
    }


    public void suggestionResultToPoiList(
            List<SuggestionResult.SuggestionInfo> suggestionInfos) {
        List<Poi> pois = new ArrayList<>();
        for (int i = 0; i < suggestionInfos.size(); i++) {
            Poi poi = new Poi();

            poi.setPoiName(suggestionInfos.get(i).getKey());
            poi.setPoiAddress(suggestionInfos.get(i).getAddress());
            poi.setImage(R.drawable.ic_pin_drop_dark_24dp);
            poi.setLatLng(suggestionInfos.get(i).getPt());
            pois.add(poi);
        }

        setStartPoiListLiveData(pois);
    }

    public void loadingData() {
        Address address = LiveDataManager
                .getInstance()
                .with("address", Address.class)
                .getValue();
        Log.d("起点：",address.getCity());
        setStartAddressLiveData(address);

    }
}
